﻿//设计模式 - 迭代器模式
//目的：循环遍历输出结果

using System.Collections;

var users = new List<User>()
{
    new(){Name="张三"},
    new(){Name="李四"},
    new(){Name="王五"},
    new(){Name="赵六"},
};
var collection = new UserCollection(users.ToArray());

while (collection.MoveNext())
{
    var user = (User)collection.Current;
    Console.WriteLine(user.Name);
}


Console.ReadKey();


interface IIterator
{
    bool HasNext();

    object Next();
}

class UserCollection : IEnumerator
{
    private readonly User[] _users;
    int index = -1;

    public UserCollection(User[] users) => this._users = users;

    public object Current => _users[index];

    public bool MoveNext()
    {
        index++;
        return index < _users.Length;
    }

    public void Reset() => index = -1;
}

class User
{
    public string Name { get; set; }
}



